package gui;

import controller.P2PController;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JFrame;
public class P2PGUI {
	
	private P2PController controller;
	private ConnectGUI connectWindow;
	private ConnectedGUI connectedWindow;
	private SelectFileGUI select;
	
	public P2PGUI(P2PController controller) {
		this.controller = controller;
		connectWindow = new ConnectGUI(this);
		connectedWindow = new ConnectedGUI(this);
	}

	

	public void connectGUI(String user_name) throws UnknownHostException{
		System.out.println("btnConnect used");
		controller.performConnect(user_name);
		connectWindow.dispose();
		connectWindow.setLogin("");
		connectedWindow.setVisible(true);
	}
	


	public void disconnectGUI(){
		System.out.println("btnDisconnect used");
		controller.performDisconnect(connectWindow.getLogin());
		connectedWindow.dispose();
		connectWindow.setVisible(true);
	}



	public void sendMessage(String messageToSend, User user) {
		System.out.println("btnAnswer used");
		controller.performAnswer(messageToSend, user);
	}

	

	public void addToUserList(User user) {
		this.connectedWindow.addToList(user);
	}
	
	public void removeFromUserList(InetAddress ip){
		this.connectedWindow.removeFromList(ip);
	}

	
	public void addToListOfMessages(String ip, String messageData) throws UnknownHostException {
		connectedWindow.updateListMessages(ip, messageData);
	}

	public void addToListOfMessages(String messageToSend, User remoteUser) {
		connectedWindow.updateListMessages(messageToSend, remoteUser);
	}



	public void selectFile() {
		select = new SelectFileGUI(this);
		select.setVisible(true);
	}

	public void treatFileSelected(String filepath){
		this.connectedWindow.setFileSelected(true);
		this.connectedWindow.setFilepath(filepath);
		this.select.dispose();
		this.connectedWindow.writeInFileArea("Do you really want to send : \n" + filepath + " ? \n");
	}


	public void sendFile(String filePath, User user) {
        controller.performSendFile(filePath,user);
	}
	
	public void setACK(String text) {
		this.connectedWindow.updateHistory(text);
	}
	
	// GETTERS & SETTERS
	
	public ConnectedGUI getConnectedWindow() {
		return connectedWindow;
	}

	
}
